package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品
 */
@Slf4j
@RestController("adminDishController")
@RequestMapping("/admin/dish")
@Api(tags = "菜品接口")
public class DishController {
	
	@Autowired
	private DishService dishService;
	
	/**
	 * 分页查询
	 */
	@GetMapping("/page")
	@ApiOperation("分页查询")
	public Result<PageResult> getDish(DishPageQueryDTO dishPageQueryDTO) {
		log.info("分页查询：{}", dishPageQueryDTO);
		return Result.success(dishService.getDish(dishPageQueryDTO));
	}
	
	/**
	 * 新增菜品
	 */
	@PostMapping
	@ApiOperation("新增菜品")
	public Result addDish(@RequestBody DishDTO dishDTO) {
		log.info("新增菜品：{}", dishDTO);
		dishService.addDish(dishDTO);
		return Result.success();
	}
	
	/**
	 * 根据id查询数据
	 */
	@GetMapping("/{id}")
	@ApiOperation("根据id查询")
	@Cacheable(cacheNames = "dishCache", key = "#id")
	public Result<DishVO> getDishId(@PathVariable Long id) {
		log.info("根据id查询：{}", id);
		return Result.success(dishService.getDishId(id));
	}
	
	/**
	 * 根据分类id查询
	 */
	@GetMapping("/list")
	@ApiOperation("根据分类id查询")
	@Cacheable(cacheNames = "dishCache", key = "#categoryId")
	public Result<List<DishVO>> getCategoryId(Long categoryId) {
		log.info("根据分类id查询：{}", categoryId);
		return Result.success(dishService.getDishCategoryId(categoryId));
	}
	
	/**
	 * 起售/停售菜品
	 */
	@PostMapping("/status/{status}")
	@ApiOperation("起售/停售菜品")
	@CacheEvict(cacheNames = "dishCache", allEntries = true)
	public Result putDishStatus(Long id, @PathVariable Integer status) {
		dishService.putDishStatus(id, status);
		return Result.success();
	}
	
	/**
	 * 删除菜品
	 */
	@DeleteMapping
	@ApiOperation("删除菜品")
	@CacheEvict(cacheNames = "dishCache", allEntries = true)
	public Result deleteDish(@RequestParam List<Long> ids) {
		log.info("删除菜品：{}", ids);
		dishService.deleteDish(ids);
		return Result.success();
	}
	
	/**
	 * 修改菜品
	 */
	@PutMapping
	@ApiOperation("修改菜品")
	@CacheEvict(cacheNames = "dishCache", allEntries = true)
	public Result putDish(@RequestBody DishDTO dishDTO) {
		log.info("修改菜品：{}", dishDTO);
		dishService.putDish(dishDTO);
		return Result.success();
	}
	
}
